package org.whatever.aha.zjut.platform.aspect;

import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.beetl.android.internal.util.ArrayUtils;
import org.springframework.stereotype.Component;
import org.whatever.aha.zjut.base.constant.ErrorCode;
import org.whatever.aha.zjut.base.constant.StatusConstant;
import org.whatever.aha.zjut.base.exception.AppException;
import org.whatever.aha.zjut.platform.annotation.CheckAcademyAdminModifyProject;
import org.whatever.aha.zjut.platform.annotation.CheckModifyQuota;
import org.whatever.aha.zjut.platform.entity.competition.CompetitionStage;
import org.whatever.aha.zjut.platform.entity.competition.CompetitionStageStatus;
import org.whatever.aha.zjut.platform.service.competition.AcademyCompetitionService;
import org.whatever.aha.zjut.platform.service.competition.CompetitionService;
import org.whatever.aha.zjut.platform.service.competition.CompetitionStageService;
import org.whatever.aha.zjut.platform.service.competition.CompetitionStageStatusService;
import org.whatever.aha.zjut.platform.util.CheckUtil;
import org.whatever.aha.zjut.platform.vo.competition.CompetitionKeyPointVo;
import org.whatever.aha.zjut.platform.vo.project.ProjectDetailVo;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Vc
 * @version 1.0
 * @Desc
 * @date 2021/11/12 20:24
 */
@Aspect
@Component
@RequiredArgsConstructor
public class CheckModifyQuotaAspect {
    final AcademyCompetitionService academyCompetitionService;
    final CompetitionStageService competitionStageService;
    final CompetitionStageStatusService competitionStageStatusService;
    final CompetitionService competitionService;

    @Pointcut("@annotation(checkModifyQuota)")
    public void pointCut(CheckModifyQuota checkModifyQuota) throws AppException {
    }

    //    @Before(value = "pointCut(checkAcademyAdminModifyProject)", argNames = "joinpoint,checkAcademyAdminModifyProject")
    @Around("@annotation(checkModifyQuota)")
    public Object before(ProceedingJoinPoint joinpoint, CheckModifyQuota checkModifyQuota) throws Throwable {
        Object[] args = joinpoint.getArgs();
        MethodSignature methodSignature = (MethodSignature) joinpoint.getSignature();
        String[] parameterNames = methodSignature.getParameterNames();
        int academyIdx = ArrayUtils.indexOf(parameterNames, "academyId");
        int academyId = (int) joinpoint.getArgs()[academyIdx];
        int quotaIdx = ArrayUtils.indexOf(parameterNames, "quota");
        int quota = (int) joinpoint.getArgs()[quotaIdx];
        int stageIdx = ArrayUtils.indexOf(parameterNames, "stageId");
        int curStageId = (int) joinpoint.getArgs()[stageIdx];
        String methodName = methodSignature.getMethod().getName();
//        //根据当前时间获取stageId
//        Integer curStageId = competitionService.getStageIdByTime(compId);

        int originalQuota = 0;
        switch (methodName){
            case "modifyAcademyThirdPrize":
                originalQuota = academyCompetitionService.getAcademyCompetitionById(academyId, curStageId).getThirdPrizeAmt();break;
            case "modifyProvinceRecAmt":
                originalQuota = academyCompetitionService.getAcademyCompetitionById(academyId, curStageId).getAcademyProvinceRecAmt();break;
            case "modifyAcademyRecNum":
                originalQuota = academyCompetitionService.getAcademyCompetitionById(academyId, curStageId).getAcademyRecAmt();break;
            default:
                throw new AppException(ErrorCode.SCHOOL_ACADEMY_MODIFY_QUOTA_FUNCTION_NOT_FOUND);
        }
        //指标内容监控，只能更大
        if(originalQuota > quota) {
            throw new AppException(ErrorCode.SCHOOL_ACADEMY_MODIFY_QUOTA_INVALID);
        }
//        //stageId 参数在最后
//        args[args.length - 1] = curStageId;
        return joinpoint.proceed(args);
    }

}
